package com.gsoc.spreadsheet.client;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.ScrollListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.TableListener;
import com.google.gwt.user.client.ui.SourcesTableEvents;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;

import java.util.Random;

public class CellGrid extends Composite {

	private int size;
	private int selCol = -1;
	private int selRow = -1;
	private DataCell[][] grid;
	private Grid display;
	
	public CellGrid() {
		size = 3;
		grid = new DataCell[size][size];
		for(int r = 0; r < size; ++r) {
			for(int c = 0; c < size; ++c) {
				setCell(r, c, new DataCell<Integer>(r, c));
			}
		}
		
		initDisplay();
	}
	
	public CellGrid(int size) {
		this.size = size;
		grid = new DataCell[size][size];
		for(int r = 0; r < size; ++r)
			for(int c = 0; c < size; ++c)
				setCell(r, c, new DataCell<Integer>(r, c));	
		
		initDisplay();
	}
	
	/*
	public CellGrid(String fileName) {
		EasyReader input = new EasyReader(fileName);
		try {
			size = input.readInt();
			
			int r = 0; 
			int c = 0;
			while((r < size) && (!input.eof())) {
				while((c < size) && (!input.eof())) {
					setCell(r, c, new DoubleDataCell(new Double(input.readInt())));
					++c;
				}
				++r;
			}
		} catch(RuntimeException e) {
			System.err.println("Error reading file!");
		}
		
	}
	*/
	
	public void setCell(int row, int col, DataCell data) {
		grid[row][col] = data;
	}
	
	public DataCell get(int row, int col) {
		return grid[row][col];
	}
	
	public int getSize() {
		return size;
	}
	
	private void initDisplay() {
		 
		int displaySize = size + 1;
		display = new Grid(displaySize, displaySize);
		display.setCellPadding(0);
		display.setCellSpacing(0);
		
		Button bu = new Button("$");
		bu.addStyleName("select-button");
		display.setWidget(0, 0, bu);
		
		// Set the top row to be column select buttons
		for(int c = 1; c <= size; ++c) {
			display.setWidget(0, c, new Button(c + ""));
		}
		
		for(int r = 1; r <= size; ++r) {
			display.setWidget(r, 0, new Button(r + ""));
		}
		
		// Set the rest of the grid
		for(int r = 1; r <= size; ++r) {
			for(int c = 1; c <= size; ++c) {
				display.setWidget(r, c, grid[r-1][c-1].view());
			}
		}
	}
	
	public Grid view() {
		return display;
	}
	
	public void selectCol(int col) {
		int c = col - 1;
		if(selCol != -1) {
			for(int r = 1; r < size; ++r) {
				grid[r-1][c].removeStyleName("selected");
			}
		}
		
		for(int r = 1; r < size; ++r) {
			grid[r-1][c].addStyleName("selected");
		}
		selCol = col;
	}
	
	public void selectRow(int row) {
		int r = row - 1;
		if(selRow != -1) {
			for(int c = 1; c < size; ++c) {
				grid[r][c-1].removeStyleName("selected");
			}
		}
		
		for(int c = 1; c < size; ++c) {
			grid[r][c-1].addStyleName("selected");
		}
		selRow = row;
	}
	
	public void loadRow(int shift) {
		
	}
	
	public void loadCol(int shift) {
		
	}
}
